using System;
using JNPF.Basics.Models;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JNPF.Utils;
using System.Transactions;

namespace JNPF.Basics.Logic
{
    public class CustomerneedBll
    {
        private CustomerneedIService service = new CustomerneedService();




        private CustomerbaseIService CustomerbaseService = new CustomerbaseService();


        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<CustomerneedEntity> GetInfo(int id)
        {
            try
            {
                return await service.GetInfo(id);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }


        /// <summary>
        /// 根据流水号 获取 信息
        /// </summary>
        /// <param name="serialid"></param>
        /// <returns></returns>
        public async Task<CustomerneedEntity> GetInfoBySerialid(string serialid)
        {
            try
            {
                return await service.GetInfoBySerialid(serialid);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }



        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<CustomerneedListOutput>> GetPageList(CustomerneedListQueryInput input)
        {
            try
            {
                return await service.GetPageList(input);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }


        /// <summary>
        /// 企业用户  ===  手机号码对应起来的 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageResult<CustomerneedListOutput>> GetBusPageList(CustomerneedListQueryInput input)
        {
            try
            {
                return await service.GetBusPageList(input);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }

        }
        /// <summary>
        /// 客户经理列表（分页）
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<CustomerneedListOutput>> GetCuseBusList(CustomerneedListQueryInput input)
        {
            try
            {
                return await service.GetCuseBusList(input);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 信贷管家   列表（分页）
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<CustomerneedListOutput>> GetPagCuseList(CustomerneedListQueryInput input)
        {

            try
            {
                return await service.GetPagCuseList(input);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }


        }





        /// <summary>
        /// 创建信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(CustomerneedEntity entity)
        {
            try
            {
                entity.CreatorTime = DateTime.Now;
                entity.CreatorUserId = UserProvider.Instance.Get().UserId;
                entity.CreatorUser = UserProvider.Instance.Get().UserName;
                entity.DeleteMark = 0;
                entity.Type = 0;
                entity.Serialid = Guid.NewGuid().ToString("N");
                int exNumber = await service.Create(entity);
                if (exNumber > 0)
                {
                    //执行成功后，推送一下 消息
                    CustomerneedEntity customerneedEntity = await GetInfoBySerialid(entity.Serialid);
                    CustomerbaseEntity CustomerbaseEntity = await CustomerbaseService.GetInfo(customerneedEntity.CustomerId);

                    List<jiushiZzhuserEntity> jiushiZzhusers = await new jiushiZzhuserBll().GetListByType(1);

                    foreach (var item in jiushiZzhusers)
                    {
                        WeChatHttpHelper.SendtemplateMessage(item.OpenId, customerneedEntity.Id, customerneedEntity.NeedAmount.ToString() + "W", customerneedEntity.CreatorTime.ToDateTimeString(), CustomerbaseEntity.CustomerName);
                    }
                }
                return exNumber;

            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Update(int id, CustomerneedEntity entity)
        {
            try
            {
                entity.Id = id;
                entity.LastModifyTime = DateTime.Now;
                entity.LastModifyUserId = UserProvider.Instance.Get().UserId;
                return await service.Update(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Delete(CustomerneedEntity entity)
        {
            try
            {
                entity.DeleteMark = 1;
                entity.DeleteTime = DateTime.Now; ;
                entity.DeleteUserId = UserProvider.Instance.Get().UserId;

                return await service.Update(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="input">过滤条件</param>
        /// <returns></returns>
        public async Task<List<CustomerneedEntity>> GetList(CustomerneedListQueryInput input)
        {
            try
            {
                return await service.GetList(input);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }


    }
}